package com.example.springboot_redis.controller;

import com.example.springboot_redis.util.RedisHashUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: guoc
 * @createDate: 2020/7/20 15:55
 */
@RestController
public class RedisController {

    @Autowired
    RedisHashUtil redisHashUtil;


    @RequestMapping(value = "/addValue")
    @ResponseBody
    public String addValue(HttpServletRequest request){
        try {
            String redisKey = request.getParameter("redisKey");
            String mapKey = request.getParameter("mapKey");
            String mapValue = request.getParameter("mapValue");
            redisHashUtil.addMapOne(redisKey,mapKey,mapValue);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }

    @RequestMapping(value = "/addMap")
    @ResponseBody
    public String addMap(HttpServletRequest request){
        try {
            String redisKey = request.getParameter("redisKey");
            String mapKey = request.getParameter("mapKey");
            String mapValue = request.getParameter("mapValue");
            Map<String,Object> redisMap=new HashMap();
            redisMap.put(mapKey, mapValue);
            redisHashUtil.addMapAll(redisKey,redisMap);
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }

    @RequestMapping(value = "/getMapAll")
    @ResponseBody
    public String getMapAll(HttpServletRequest request){
        try {
            String redisKey = request.getParameter("redisKey");
            Map<String, Object> mapAll = redisHashUtil.getMapAll(redisKey);
            return mapAll.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }

    @RequestMapping(value = "/getMapValues")
    @ResponseBody
    public String getMapValues(HttpServletRequest request){
        try {
            String redisKey = request.getParameter("redisKey");
            List<Object> mapValues = redisHashUtil.getMapValues(redisKey);
            return mapValues.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }

    @RequestMapping(value = "/hasKey")
    @ResponseBody
    public String hasKey(HttpServletRequest request){
        try {
            String redisKey = request.getParameter("redisKey");
            String mapKey = request.getParameter("mapKey");
            boolean b = redisHashUtil.hasKey(redisKey, mapKey);
            if (b){
                return "YES";
            }else {
                return "NO";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }


    @RequestMapping(value = "/getMapVal")
    @ResponseBody
    public String getMapVal(HttpServletRequest request){
        try {
            String redisKey = request.getParameter("redisKey");
            String mapKey = request.getParameter("mapKey");
            String mapVal = (String)redisHashUtil.getMapVal(redisKey, mapKey);
            return mapVal;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "fail";
    }





}
